import javax.swing.*;
import java.awt.*;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;

/**
 * Created by user on 16.02.14.
 */
public class CreateFrame extends JFrame {
    JFrame frame;
    JTextArea inField;
    JTextArea keyField;
    JTextArea outField;

    public CreateFrame() {


        frame = new JFrame("Shifrator");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setLocation(350, 200);
        JPanel mainPanel = new JPanel(new BorderLayout());


        // Create north side for mainPanel
        JPanel north = new JPanel();
        JPanel inputTextPanel = new JPanel();
        JLabel textIN = new JLabel("Input text:");
        inField = new JTextArea(5, 30);
        inputTextPanel.add(textIN);
        inputTextPanel.add(inField);
        north.add(inputTextPanel, BorderLayout.CENTER);
        mainPanel.add(north, BorderLayout.NORTH);
        frame.add(mainPanel);

        JPanel center = new JPanel();
        Box keyType = new Box(BoxLayout.LINE_AXIS);
        JLabel key = new JLabel("Key");
        keyField = new JTextArea(1, 5);
        JLabel type = new JLabel("Type");
        ButtonGroup bg = new ButtonGroup();
        JRadioButton cezar = new JRadioButton("cezar");
        JRadioButton viginer = new JRadioButton("viginer");
        bg.add(cezar);
        bg.add(viginer);


        keyType.add(key);
        keyType.add(keyField);
        keyType.add(type);
        keyType.add(cezar);
        keyType.add(viginer);
        center.add(keyType, BorderLayout.CENTER);
        mainPanel.add(center, BorderLayout.CENTER);

        JPanel south = new JPanel();
        JPanel outTextPanel = new JPanel();
        JLabel textOUT = new JLabel("Output text:");
        outField = new JTextArea(5, 30);
        outTextPanel.add(textOUT, BorderLayout.WEST);
        outTextPanel.add(outField, BorderLayout.EAST);
        south.add(outTextPanel, BorderLayout.CENTER);
        JButton encrypt = new JButton("Encrypt");
        encrypt.setSize(10, 10);
        if (cezar.isSelected()) {
            encrypt.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    ShifrManager.cezarShifr(inField.getText(), Integer.parseInt(keyField.getText()));
                    outField.setText(ShifrManager.cezarShifr(inField.getText(), Integer.parseInt(keyField.getText())));
                }
            });
        }
        if (viginer.isSelected()) {
            encrypt.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    ShifrManager.viginerEncrypt(inField.getText(), keyField.getText());
                    outField.setText(ShifrManager.viginerEncrypt(inField.getText(), keyField.getText()));
                }
            });
        }


        JButton decrypt = new JButton("Decrypt");
        decrypt.setSize(10, 10);
        if (cezar.isSelected()) {
            decrypt.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    ShifrManager.cezarDeshifr(inField.getText(), Integer.parseInt(keyField.getText()));
                    outField.setText(ShifrManager.cezarDeshifr(inField.getText(), Integer.parseInt(keyField.getText())));
                }
            });
        }
        if (viginer.isSelected()) {
            decrypt.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    ShifrManager.viginerDecrypt(inField.getText(), keyField.getText());
                    outField.setText(ShifrManager.viginerDecrypt(inField.getText(), keyField.getText()));
                }
            });
        }
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(encrypt, BorderLayout.WEST);
        buttonPanel.add(decrypt, BorderLayout.EAST);
        south.add(buttonPanel, BorderLayout.SOUTH);
        mainPanel.add(south, BorderLayout.SOUTH);


        frame.pack();
        frame.setVisible(true);
    }
}

